package com.minhua.jijiehao2.mainframe.account.accountlistfragment.impl

import android.os.Bundle
import android.util.Log
import android.view.View
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.abslistview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.AccountBean
import com.minhua.jijiehao2.bean.AllAccountBean
import com.minhua.jijiehao2.mainframe.account.accountFragment.imp.IAccountView
import com.minhua.jijiehao2.mainframe.account.accountFragment.impl.AccountPresenter
import com.minhua.jijiehao2.mainframe.account.addaccountactivity.impl.AddAccountActivity
import com.minhua.jijiehao2.utils.T
import com.minhua.jijiehao2.view.AccountDetailDialog
import kotlinx.android.synthetic.main.fragment_account_list.*

class AccountListFragment : BaseFragment<IAccountView, AccountPresenter>(), IAccountView {

    override fun getLayoutId(): Int = R.layout.fragment_account_list

    override fun getPresenter(): AccountPresenter = AccountPresenter()

    companion object {

        fun newInstance(repayStatus: Int): AccountListFragment {
            val fragment = AccountListFragment()
            val args = Bundle()
            args.putInt("repayStatus", repayStatus)
            fragment.arguments = args
            return fragment
        }
    }

    private var isFirstIn = true
    override fun onResume() {
        super.onResume()
        if (isFirstIn) {
            isFirstIn = false
        } else {
            initData()
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        Log.e("AccountListFragment", "onHiddenChanged")
    }

    private var repayStatus: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            repayStatus = arguments.getInt("repayStatus")
        }
    }

    private val listAccount = arrayListOf<AccountBean>()
    private var accountAdapter: BaseCommonAdapter<AccountBean>? = null

    override fun initView() {
        accountAdapter = object : BaseCommonAdapter<AccountBean>(activity, R.layout.item_list_account, listAccount) {
            override fun convert(holder: ViewHolder?, t: AccountBean?) {
                holder?.setOnClickListener(R.id.tvGoldDetail, { showDetailDialog(t) })

                holder?.setText(R.id.tvGoldName, t?.pName + "(" + t?.ordersName + ")")
                holder?.setText(R.id.tvGoldCount, t?.amtByMonth.toString())
                holder?.setText(R.id.tvGoldDay, t?.remainRepayDay)
                Glide.with(activity).load(t?.picUrl).into(holder?.getView(R.id.ivGoldIcon))

                val repayStatus = t?.repayStatus
                if (repayStatus == 0) {
                    holder?.setVisible(R.id.tvGoldStatus, View.INVISIBLE)
                } else {
                    holder?.setVisible(R.id.tvGoldStatus, View.VISIBLE)
                }
            }
        }
        slvContent.adapter = accountAdapter

        refresh.setOnRefreshListener { mPresenter.getAccountList() }

        slvContent.setOnItemClickListener { _, _, position, _ ->
            AddAccountActivity.intentStart(activity, listAccount[position])
        }
    }

    override public fun initData() {
        super.initData()
        mPresenter.getAccountList()
    }

    /**
     * 展示详情dialog
     */
    private var dialog: AccountDetailDialog? = null

    private fun showDetailDialog(t: AccountBean?) {
        if (dialog == null) {
            dialog = AccountDetailDialog(activity)
        }
        dialog?.setData(t)
        dialog?.show()
    }

    override fun onAccountListSuccess(allAccountBean: AllAccountBean) {
        refresh.isRefreshing = false
        listAccount.clear()
        val list = allAccountBean.accountRecords.filter { it.repayStatus == repayStatus }
        listAccount.addAll(list)
        accountAdapter?.notifyDataSetChanged()
        if (listAccount.isEmpty()) {
            ivEmpty.visibility = View.VISIBLE
            slvContent.visibility = View.GONE
        } else {
            ivEmpty.visibility = View.GONE
            slvContent.visibility = View.VISIBLE
        }
    }

    override fun onDeleteSuccess() {
        T.showShort("删除成功")
        mPresenter.getAccountList()
    }

    override fun onRepaySuccess() {
        T.showShort("操作成功")
        mPresenter.getAccountList()
    }
}
